<?php
namespace app\common\controller;

use think\Controller;
use think\Db;
use think\cache\driver\Redis;

class Wfhk6 extends Controller
{
    protected $czid;
    protected $time;
    protected $redis;

    public function _initialize()
    {
        parent::_initialize();
        header("Content-type: text/html; charset=utf-8");
        set_time_limit(0);
        $this->czid = '73';
        $this->time = time();
        $this->redis = new Redis();
    }

    /**
     * 获取开奖数据并执行后续操作
     */
    public function index()
    {
       
		$arr = array('01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31','32','33','34','35','36','37','38','39','40','41','42','43','44','45','46','47','48','49');
		shuffle($arr);
        $opencode=$arr[0].$arr[1].$arr[2].$arr[3].$arr[4].$arr[5].$arr[6];

	
		echo "---------- 开始处理当期数据 ----------<br/><br/>";
			//获取最新 开奖期号
			$info = Db::name('kjjg_list')
				->where(['czid' => $this->czid, 'kjjg' => '0'])
				->field('qihao,jztime')
				->where('jztime', '<', $this->time)
				->order('id', 'DESC')
				->find();
				
			if(!$info) {//如果没有，生成应该开奖的期号	
				
				$beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
			    $qihao = floor((time()- $beginToday)/300);
				$this->sprintf_num($qihao);
				$ymd =  date('Ymd',$this->time);
			 	$info['qihao'] = $ymd.$qihao;
                $info['jztime'] = $this->time;
				
			}	 
			$que = Db::name('caipiao')->where(['czid'=>$this->czid,'qihao'=>$info['qihao']])->find();
            if($que){
                $opencode = explode(',', $que['kjjg']);
                $opencode = implode('', $opencode);
            }
			/*		$result = array(
						'qihao' => $info['qihao'],
						'czid' => $this->czid,
						'opencode' => $opencode,
						'pentime' => date('Y-m-d H:i:s', $this->time),
						'opentimestamp' => $this->time,
					);

					$result_json = json_encode($result);
					$this->insertDataRedis($result_json);
			  */
			//开奖结果入库
			$this->kjjgSave($this->czid, $info['qihao'], $info['jztime'], $opencode);
			//将最新开奖数据存入Redis
			$this->redis->set("result_{$this->czid}", "{$info['qihao']},{$info['jztime']},{$opencode}");

        echo "<br/>---------- 处理当期数据完成 ----------<br/>";
    }

    /**
     * 执行开奖数据入库操作
     */
    private function kjjgSave($czid, $qihao, $jztime, $kjjg_string)
    {
        // 查询是否存在本期
        $exist = Db::name('kjjg_list')->where(['qihao' => $qihao, 'czid' => $czid])->find();

        if ($exist) {
            if (!$exist['iftz']) {
                Db::name('kjjg_list')->where('id', $exist['id'])->data(['kjjg' => $kjjg_string, 'iftz' => 1])->update();
                echo "{$czid} 号彩第 {$qihao} 期数据更新成功<br/>";
            }
        } else {
            Db::name('kjjg_list')->data(['czid' => $czid, 'qihao' => $qihao, 'jztime' => $jztime, 'kjjg' => $kjjg_string, 'iftz' => 1])->insert();
            echo "{$czid} 号彩第 {$qihao} 期数据写入成功<br/>";
        }
    }
    //补0
	public function sprintf_num($num){
		  $num=sprintf("%03d",$num);
	 return   $num;
	}
    /**
     * 最新开奖数据写入Redis
    
    private function insertDataRedis($data) {
        $this->redis->set('Wfhk6_kjjg',"{$data}");
        
    }
   */
   
}
